﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Utilities
{
    public class DateTimeRange
    {
        private DateRange _dateRange;

        private DateTime _lowerRange;

        public DateTime LowerRange
        {
            get 
            { 
                return _lowerRange; 
            }
            set
            {
                this._lowerRange = value;
            }
            
        }

        private DateTime _upperRange;

        public DateTime UpperRange
        {
            get 
            { 
                return _upperRange; 
            }
            set
            {
                this._upperRange = value;
            }
        }


        public DateTimeRange()
        {

        }

        public DateTimeRange(DateRange dateRange)
        {
            this._dateRange = dateRange;
            SetRanges();
        }

        private void SetRanges()
        {
            switch (this._dateRange)
            {
                case DateRange.Today:
                    {
                        this._lowerRange = DateTime.Today;
                        this._upperRange = DateTime.Today;
                        this._upperRange = this._upperRange.Add(new TimeSpan(0, 23, 59, 59, 999));
                        break;
                    }
                case DateRange.Yesterday:
                    {
                        this._lowerRange = DateTime.Today.Subtract(new TimeSpan(1, 0, 0, 0));
                        this._upperRange = DateTime.Today.Subtract(new TimeSpan(1, 0, 0, 0));
                        this._upperRange = this._upperRange.Add(new TimeSpan(0, 23, 59, 59, 999));
                        break;
                    }
                case DateRange.DayBeforeYesterday:
                    {
                        this._lowerRange = DateTime.Today.Subtract(new TimeSpan(2, 0, 0, 0));
                        this._upperRange = DateTime.Today.Subtract(new TimeSpan(2, 0, 0, 0));
                        this._upperRange = this._upperRange.Add(new TimeSpan(0, 23, 59, 59, 999));
                        break;
                    }
                case DateRange.ThisWeek:
                    {
                        DayOfWeek dayOfWeek = DateTime.Today.DayOfWeek;
                        switch (dayOfWeek)
                        {
                            case DayOfWeek.Monday:
                                {
                                    this._lowerRange = DateTime.Today;
                                    this._upperRange = this._lowerRange.Add(new TimeSpan(6, 23, 59, 59, 999));
                                    break;
                                }
                            case DayOfWeek.Tuesday:
                                {
                                    this._lowerRange = DateTime.Today.Subtract(new TimeSpan(1,0,0,0));
                                    this._upperRange = this._lowerRange.Add(new TimeSpan(6, 23, 59, 59, 999));
                                    break;
                                }
                            case DayOfWeek.Wednesday:
                                {
                                    this._lowerRange = DateTime.Today.Subtract(new TimeSpan(2, 0, 0, 0)); ;
                                    this._upperRange = this._lowerRange.Add(new TimeSpan(6, 23, 59, 59, 999));
                                    break;
                                }
                            case DayOfWeek.Thursday:
                                {
                                    this._lowerRange = DateTime.Today.Subtract(new TimeSpan(3, 0, 0, 0)); ;
                                    this._upperRange = this._lowerRange.Add(new TimeSpan(6, 23, 59, 59, 999));
                                    break;
                                }
                            case DayOfWeek.Friday:
                                {
                                    this._lowerRange = DateTime.Today.Subtract(new TimeSpan(4, 0, 0, 0)); ;
                                    this._upperRange = this._lowerRange.Add(new TimeSpan(6, 23, 59, 59, 999));
                                    break;
                                }
                            case DayOfWeek.Saturday:
                                {
                                    this._lowerRange = DateTime.Today.Subtract(new TimeSpan(5, 0, 0, 0)); ;
                                    this._upperRange = this._lowerRange.Add(new TimeSpan(6, 23, 59, 59, 999));
                                    break;
                                }
                            case DayOfWeek.Sunday:
                                {
                                    this._lowerRange = DateTime.Today.Subtract(new TimeSpan(6, 0, 0, 0)); ;
                                    this._upperRange = this._lowerRange.Add(new TimeSpan(6, 23, 59, 59, 999));
                                    break;
                                }
                        }
                        break;
                    }
                case DateRange.LastWeek:
                    {
                        DayOfWeek dayOfWeek = DateTime.Today.DayOfWeek;
                        switch (dayOfWeek)
                        {
                            case DayOfWeek.Monday:
                                {
                                    this._lowerRange = DateTime.Today.Subtract(new TimeSpan(7,0,0,0));
                                    this._upperRange = this._lowerRange.Add(new TimeSpan(6, 23, 59, 59, 999));
                                    break;
                                }
                            case DayOfWeek.Tuesday:
                                {
                                    this._lowerRange = DateTime.Today.Subtract(new TimeSpan(8, 0, 0, 0));
                                    this._upperRange = this._lowerRange.Add(new TimeSpan(6, 23, 59, 59, 999));
                                    break;
                                }
                            case DayOfWeek.Wednesday:
                                {
                                    this._lowerRange = DateTime.Today.Subtract(new TimeSpan(9, 0, 0, 0)); ;
                                    this._upperRange = this._lowerRange.Add(new TimeSpan(6, 23, 59, 59, 999));
                                    break;
                                }
                            case DayOfWeek.Thursday:
                                {
                                    this._lowerRange = DateTime.Today.Subtract(new TimeSpan(10, 0, 0, 0)); ;
                                    this._upperRange = this._lowerRange.Add(new TimeSpan(6, 23, 59, 59, 999));
                                    break;
                                }
                            case DayOfWeek.Friday:
                                {
                                    this._lowerRange = DateTime.Today.Subtract(new TimeSpan(11, 0, 0, 0)); ;
                                    this._upperRange = this._lowerRange.Add(new TimeSpan(6, 23, 59, 59, 999));
                                    break;
                                }
                            case DayOfWeek.Saturday:
                                {
                                    this._lowerRange = DateTime.Today.Subtract(new TimeSpan(12, 0, 0, 0)); ;
                                    this._upperRange = this._lowerRange.Add(new TimeSpan(6, 23, 59, 59, 999));
                                    break;
                                }
                            case DayOfWeek.Sunday:
                                {
                                    this._lowerRange = DateTime.Today.Subtract(new TimeSpan(13, 0, 0, 0)); ;
                                    this._upperRange = this._lowerRange.Add(new TimeSpan(6, 23, 59, 59, 999));
                                    break;
                                }
                        }
                        break;
                    }
                case DateRange.ThisMonth:
                    {
                        this._lowerRange = DateTime.Today.AddDays(1 - DateTime.Today.Day);
                        this._upperRange = this._lowerRange.AddMonths(1).AddMilliseconds(-1);
                        break;

                    }
                default:
                    {
                        break;
                    }
            }

            if (this._upperRange.Date.CompareTo(DateTime.Today.Date) > 0)
            {
                this._upperRange = DateTime.Today.Date.Add(new TimeSpan(0, 23, 59, 59, 999));
            }
        }
    }
}
